package Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import mx.rpc.events.ResultEvent;
	import Welsy4.Connect.business.PrincipalListDelegate;
	import Welsy4.Connect.events.PrincipalListEvent;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.events.EventChainFactory ;
	import Welsy4.Connect.events.UserEvent;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.User;
	
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;

	public class PrincipalListCommand extends SequenceCommand implements Command, IResponder
	{
		public function PrincipalListCommand()
		{
		}

		override public function execute(event:CairngormEvent):void
		{
			ConnectModel.getInstance().addQueue("PrincipalListCommand.execute/nlogin : "+ ChainEvent(event).eventRequest.login);
			this.nextEvent = ChainEvent(event).nextChainedEvent;
			
			var delegate : PrincipalListDelegate = new PrincipalListDelegate(this);
			var le : PrincipalListEvent = PrincipalListEvent(event);
			
			/* memorizza lastUserSearched in ConnectModel*/
			ConnectModel.getInstance().lastUserSearched = new User(ChainEvent(event).eventRequest.login);
			delegate.findUserByLogin(ChainEvent(event).eventRequest.login);
			
			
		}
		
		public function result(event : Object):void
		{
			// response XML
			ConnectModel.getInstance().addQueue("PrincipalListCommand.result");
			
			ConnectModel.getInstance().isLogged = true;
			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(event.result);
			
			// verify status response
			if (ConnectModel.getInstance().httpServiceHandler.getResultStatus() == ConnectModel.SESSION_OK) {
				if (ConnectModel.getInstance().httpServiceHandler.xml.elements("principal-list").principal.length() > 0) {
					//Alert.show("User exists, ok");
				    
					// memorizza id utente trovato
					ConnectModel.getInstance().lastUserFound = ConnectModel.getInstance().httpServiceHandler.getLastUserFound();
					this.executeNextCommand();
				} else {
					// crea utente
					//Alert.show("User does not exist! Creating user...");
					//var chainNextEvent:ChainEvent = EventChainFactory.reconstructChainEvent(ChainEvent(this.nextEvent));
					 
					var user:User = ConnectModel.getInstance().lastUserSearched;
					var newChainEvent:ChainEvent = EventChainFactory.chainEvents([new UserEvent(UserEvent.CREATE_USER, user), this.nextEvent]);
			        
					CairngormEventDispatcher.getInstance().dispatchEvent(newChainEvent);
					
				}
				
			} else {
			    CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.INVALID, event.result));
			}
			
			
			
		    
			
		}
		
		public function fault(event : Object):void
		{
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.LOGIN_COMMAND_FAULT, event));
			ConnectModel.getInstance().addQueue("LoginCommand.fault");
		}
		
	}
}